package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.utils.UtilFuns;
import com.itheima.dao.system.ModuleDao;
import com.itheima.dao.system.RoleDao;
import com.itheima.doamin.system.Role;
import com.itheima.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleDao roleDao;
    @Autowired
    ModuleDao moduleDao;

    @Override
    public PageInfo findAll(String companyId,int page,int size) {
        //进行分页查询
        PageHelper.startPage(page,size);
        List<Role> roles = roleDao.findAll(companyId);
        return new PageInfo(roles);
    }

    @Override
    public List<Role> findAll(String companyId){
        return roleDao.findAll(companyId);
    }

    @Override
    public Role findById(String roleId) {
        return roleDao.findById(roleId);
    }

    @Override
    public void delete(String roleId) {
        roleDao.delete(roleId);
    }

    @Override
    public void save(Role role) {
        role.setId(UtilFuns.getUUID());
        roleDao.save(role);
    }

    @Override
    public void update(Role role) {
        roleDao.update(role);
    }

    @Override
    public List<Map> findRoleModule(String id) {
        return moduleDao.findRoleModule(id);
    }

    @Override
    public void updateRoleModule(String roleid, String moduleIds) {
        //更新权限
        roleDao.deleteAllRoleModule(roleid);
        String[] moduleIdArr=moduleIds.split(",");
        for (String moduleId:moduleIdArr){
            //重新插入角色的权限
            roleDao.updateRoleModule(roleid,moduleId);
        }
    }
}
